#include<iostream>
#include<ctime>
#include"task.hpp"
#include"SerCliMode.hpp"
using std::cout;
using std::endl;

const std::string ops= "+-*/%";

void* productor(void* argc)
{
    blockQueue<Task>* bBq=static_cast<blockQueue<Task>*>(argc);
    while(true)
    {
        int eleOne=rand()%50;
        int eleTwo=rand()%10;
        char oper=ops[rand()%4];
        Task t(eleOne,eleTwo,oper);

        bBq->push(t);
        cout<<"productor["<<pthread_self()<<"]"<<eleOne<<oper<<eleTwo<<"=?"<<endl;
        sleep(1);
    }
    return nullptr;
}

void* consumer(void* argc)
{
    blockQueue<Task>*bBq=static_cast<blockQueue<Task>*>(argc);
    while(true)
    {
        Task t=bBq->pop();
        int result=t();
        int eleOne,eleTwo;
        char oper;
        t.get(&eleOne,&eleTwo,&oper);
        cout<<"consumer["<<pthread_self()<<"]"<<(unsigned long)time(nullptr)<<"consumed a task:"<<eleOne<<oper<<eleTwo<<"="<<result<<endl;
    }
    return nullptr;
}

int main()
{
    srand((unsigned long)time(nullptr)^getpid());
    blockQueue<Task> bq;
    pthread_t pro,con;
    pthread_create(&pro,nullptr,productor,&bq);
    pthread_create(&con,nullptr,consumer,&bq);
    pthread_join(pro,nullptr);
    pthread_join(con,nullptr);
    return 0;
}
